﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Runtime.InteropServices;
using System.IO;

namespace GenerateMovieList
{
    public partial class ListXML : Form
    {
        private static Popup _popup = new Popup();

        public ListXML()
        {
            InitializeComponent();
        }
        public ListXML(string loadxmlfile)
        {
            InitializeComponent();
            createdgv();
            popdgv(loadxmlfile);
            xmlfile = loadxmlfile;
        }



        [DllImport("user32.dll")]
        static extern bool GetCursorPos(ref Point lpPoint);

        private string rowid = "";
        private string xmlfile = "";
        private string moviename = "";
        private string overview = "";
        private string url = "";
        private string imdbid = "";

        private void createdgv()
        {
            List<string> Movie = new List<string>();
            Movie.Add("Filename");
            Movie.Add("Extension");
            Movie.Add("Name");
            Movie.Add("Alternative Name");
            Movie.Add("Released");
            Movie.Add("Overview");
            Movie.Add("URL");
            Movie.Add("Rating");
            Movie.Add("Runtime (min)");
            Movie.Add("Certification");
            Movie.Add("Studio(s)");
            Movie.Add("Categor(y/ies)");
            Movie.Add("ID");
            Movie.Add("IMDBID");

            foreach (string item in Movie)
            {
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                column.HeaderText = item;
                column.Name = item;
                column.ValueType = typeof(DataGridViewTextBoxColumn);
                column.ReadOnly = true;
                dgvListXml.Columns.Add(column);
            }
            dgvListXml.RowHeadersVisible = false;
            dgvListXml.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvListXml.Columns[5].Visible = false;
            dgvListXml.Columns[6].Visible = false;
            dgvListXml.Columns[12].Visible = false;
            dgvListXml.Columns[13].Visible = false;
        }
        private void popdgv(string xmlfile)
        {
            if (xmlfile != string.Empty)
            {
                dgvListXml.Rows.Clear();
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlfile);
                XmlNodeList elements = xmlDoc.SelectNodes("Movies/Movie");
                foreach (XmlNode element in elements)
                {
                    try
                    {
                        int n = dgvListXml.Rows.Add();
                        dgvListXml.Rows[n].Cells[0].Value = element.SelectSingleNode("Filename").InnerText;
                        dgvListXml.Rows[n].Cells[1].Value = element.SelectSingleNode("Extension").InnerText;
                        dgvListXml.Rows[n].Cells[2].Value = element.SelectSingleNode("Name").InnerText;
                        dgvListXml.Rows[n].Cells[3].Value = element.SelectSingleNode("AlternativeName").InnerText;
                        dgvListXml.Rows[n].Cells[4].Value = element.SelectSingleNode("Released").InnerText;
                        dgvListXml.Rows[n].Cells[5].Value = element.SelectSingleNode("Overview").InnerText;
                        dgvListXml.Rows[n].Cells[6].Value = element.SelectSingleNode("Url").InnerText;
                        dgvListXml.Rows[n].Cells[7].Value = element.SelectSingleNode("Rating").InnerText;
                        dgvListXml.Rows[n].Cells[8].Value = element.SelectSingleNode("Runtime").InnerText;
                        dgvListXml.Rows[n].Cells[9].Value = element.SelectSingleNode("Certification").InnerText;
                        dgvListXml.Rows[n].Cells[10].Value = element.SelectSingleNode("MovieStudios").InnerText;
                        dgvListXml.Rows[n].Cells[11].Value = element.SelectSingleNode("Categories").InnerText;
                        dgvListXml.Rows[n].Cells[12].Value = element.SelectSingleNode("ID").InnerText;
                        dgvListXml.Rows[n].Cells[13].Value = element.SelectSingleNode("IMDBID").InnerText;
                    }
                    catch { }
                }
            }
        }

        private void dgvListXml_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex != -1)
                {
                    DataGridViewRow row = dgvListXml.CurrentRow;
                    rowid = Convert.ToString(row.Cells[13].Value);
                    url = Convert.ToString(row.Cells[6].Value);
                    moviename = Convert.ToString(row.Cells[2].Value);
                    overview = Convert.ToString(row.Cells[5].Value);
                    imdbid = Convert.ToString(row.Cells[13].Value);
                    Point defPnt = new Point();
                    GetCursorPos(ref defPnt);
                    cmsRightmenu.Show(defPnt.X, defPnt.Y);
                }
            }
        }
        private void deletexmlnode(string ID,string output)
        {
            if (File.Exists(output))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(output);
                XmlNodeList elements = xmlDoc.SelectNodes("Movies/Movie");
                foreach (XmlNode element in elements)
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(element.InnerText, ID, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {
                        element.ParentNode.RemoveChild(element);
                    }
                }
                xmlDoc.Save(output);
            }
        }

        private void ListXML_FormClosed(object sender, FormClosedEventArgs e)
        {
            _popup.Close();
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            deletexmlnode(rowid, xmlfile);
            popdgv(xmlfile);
        }

        private void tsmiOverview_Click(object sender, EventArgs e)
        {
            Point defPnt = new Point();
            GetCursorPos(ref defPnt);
            if (_popup != null)
                _popup.Close();
            _popup = new Popup(moviename, overview, url, imdbid);
            _popup.Show();
            _popup.Location = new Point(defPnt.X, defPnt.Y);
        }
    }
}
